var crypto = require("crypto");

exports.rsaSign = function(prestr, key, _hash) {

	key = `-----BEGIN RSA PRIVATE KEY-----
MIICXAIBAAKBgQC/UvOmDmLUGCkMNyE7L0H9vpZTLh114pqJU6N2pQ7QOIgHnM6G
tMaOqEL9hFSXRJ0JCz3+RIHUMRhOtq0jo0XQ5ika4D/RptVoNo6/OFVs9VCvBpwY
ridJo7Df880Dy/x2Po126AbmUxqQMQOZtz7UPicI+Bt4nYmF7wGYkCFCVQIDAQAB
AoGALXK3Dis8rsVOeuhdrIF9W5l6fCfoN6isrRKBMh3jdvL23VgSd01aHWk7AJ3+
ujBCXXOeNlhvvsgqzgf2+RA/9QaDAfZCT+kNpodXiVxFWJMrfT2AdAhmvGvSmFbH
XrP+PIbXEgpht2FhWRuhJ23Soz39NT8VxRDfwPkLyQq2xgECQQDk9oVfuCvakz6P
aN82D1/DS9KNFblUd9wlHUWXDAOjkbh2CWpPwBeW0Lj7Is0lxU1klENosQAxsdtt
vAA6tZ/NAkEA1eqeCDiddGwAyiwtj9hB8HB2xM99di++OajYkFO47tj5KUdjqIb5
1WcixV0yaAB7Q1VqUvvmBrs4b7J8VnnUqQJBAMX955e+Sy3X5mFc7qhw4v6espQO
FRVWEKiHh4X2fa8Uc86elbAhDET+ePeyQHRJCYPQMfp2FRTkGDvsNsaOCIECQFC/
CPwNctHZWsr1thUQCoPZz9smfzM3flSBeY2xLKNr31FHk0Sv/mohTiy1qys600xt
V38Pepqb3Qe8ldaGklECQD/W1xcU+rbnwr8+ODTsFuRIWdBx3lNZCeAZZqEtaDX2
mk6v6W6TlWDFtUJWmLRyADYLwW5DbWPGFscnfAYJcuU=
-----END RSA PRIVATE KEY-----`;

// 	key = `-----BEGIN PRIVATE KEY-----
// MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAL9S86YOYtQYKQw3
// ITsvQf2+llMuHXXimolTo3alDtA4iAeczoa0xo6oQv2EVJdEnQkLPf5EgdQxGE62
// rSOjRdDmKRrgP9Gm1Wg2jr84VWz1UK8GnBiuJ0mjsN/zzQPL/HY+jXboBuZTGpAx
// A5m3PtQ+Jwj4G3idiYXvAZiQIUJVAgMBAAECgYAtcrcOKzyuxU566F2sgX1bmXp8
// J+g3qKytEoEyHeN28vbdWBJ3TVodaTsAnf66MEJdc542WG++yCrOB/b5ED/1BoMB
// 9kJP6Q2mh1eJXEVYkyt9PYB0CGa8a9KYVsdes/48htcSCmG3YWFZG6EnbdKjPf01
// PxXFEN/A+QvJCrbGAQJBAOT2hV+4K9qTPo9o3zYPX8NL0o0VuVR33CUdRZcMA6OR
// uHYJak/AF5bQuPsizSXFTWSUQ2ixADGx2228ADq1n80CQQDV6p4IOJ10bADKLC2P
// 2EHwcHbEz312L745qNiQU7ju2PkpR2OohvnVZyLFXTJoAHtDVWpS++YGuzhvsnxW
// edSpAkEAxf3nl75LLdfmYVzuqHDi/p6ylA4VFVYQqIeHhfZ9rxRzzp6VsCEMRP54
// 97JAdEkJg9Ax+nYVFOQYO+w2xo4IgQJAUL8I/A1y0dlayvW2FRAKg9nP2yZ/Mzd+
// VIF5jbEso2vfUUeTRK/+aiFOLLWrKzrTTG1Xfw96mpvdB7yV1oaSUQJAP9bXFxT6
// tufCvz44NOwW5EhZ0HHeU1kJ4BlmoS1oNfaaTq/pbpOVYMW1QlaYtHIANgvBbkNt
// Y8YWxyd8Bgly5Q==
// -----END PRIVATE KEY-----`
		// key='asd'
		// prestr = prestr + key;
		// return crypto.createHash('rsa').update(prestr, "utf-8").digest("hex");
		// return crypto.createHmac('RSA-SHA1', key).update(prestr, "utf-8").digest("hex");
	var sign = crypto.createSign(_hash || 'sha1WithRSAEncryption');
	sign.update(prestr,"utf-8")
	var _sign = sign.sign(key, "base64");

	return _sign
};

// console.log(crypto.createHash('RSA-SHA1').update('Gaubee', "utf-8").digest("hex"))
// console.log(crypto.createHmac('RSA-SHA1', 'sads').update('Gaubee', "utf-8").digest("hex"))

// var str = 'partner="2088511420574362"&seller_id="dotnar@hotmail.com"&out_trade_no="1216045324-3655"&subject="测试的商品"&body="该测试商品的详细描述"&total_fee="0.01"&notify_url="http://notify.msp.hk/notify.htm"&service="mobile.securitypay.pay"&payment_type="1"&_input_charset="utf-8"&it_b_pay="30m"&return_url="m.alipay.com"'
// console.log(crypto.getHashes().map((_hash) => {
// 	try {
// 		return `${_hash}: 	${exports.rsaSign(str, null, _hash)}`
// 	} catch (e) {
// 		return `Error: ${_hash}`
// 	}
// }))

// console.log(exports.rsaSign('partner="2088511420574362"&seller_id="dotnar@hotmail.com"&out_trade_no="121615530111088"&subject="测试的商品"&body="该测试商品的详细描述"&total_fee="0.01"&notify_url="http://notify.msp.hk/notify.htm"&service="mobile.securitypay.pay"&payment_type="1"&_input_charset="utf-8"&it_b_pay="30m"&return_url="m.alipay.com"'))